/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.template;


import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.themes.Reindeer;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ow2.bonita.facade.runtime.ActivityState;
import org.ow2.bonita.light.LightProcessDefinition;
import org.ow2.bonita.light.LightTaskInstance;
import org.processbase.touchprocess.ui.TouchProcessApplication;

/**
 *
 * @author marat
 */
public class TaskButton extends CssLayout {

    private static final long serialVersionUID = 1L;
    private LightTaskInstance task;
    private static final String STYLENAME = "message-button";
    
    public TaskButton(LightTaskInstance task) {
        try {
            this.task = task;
            
            setWidth("100%");
            setStyleName(STYLENAME);
            
            LightProcessDefinition lpd = TouchProcessApplication.getCurrent().getBpmModule().getLightProcessDefinition(task.getProcessDefinitionUUID());
            String processName = lpd.getLabel() != null ? lpd.getLabel() : lpd.getName();
            String processInstanceUUID = task.getProcessInstanceUUID().toString();
            String processFullName = processName + "  #" + processInstanceUUID.substring(processInstanceUUID.lastIndexOf("--") + 2);
            String taskTitle = task.getDynamicLabel() != null ? task.getDynamicLabel() : task.getActivityLabel();
            String taskDescription = task.getDynamicDescription() != null ? (task.getDynamicDescription()) : "";
            
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm:ss");
            Label lbl = new Label(formatter.format(task.getLastUpdateDate()));
            lbl.setStyleName(STYLENAME + "-lastupdate");
            lbl.addStyleName(Reindeer.LABEL_SMALL);
            lbl.setWidth("-1px");
            addComponent(lbl);
            
//            lbl = new Label(task.getExpectedEndDate() != null ? formatter.format(task.getExpectedEndDate()) : " "); // FIXME Should be date from message
//            lbl.setWidth("-1px");
//            lbl.setStyleName(STYLENAME + "-time");
//            lbl.addStyleName(Reindeer.LABEL_SMALL);
//            addComponent(lbl);
            
            Label process = new Label();
            process.setHeight("1.5em");
            process.setWidth("-1px");
            process.setStyleName(STYLENAME + "-process");
            if (processFullName.length() > 35) {
                process.setValue(processFullName.substring(0, 35) + "...");
            } else {
                process.setValue(processFullName);
            }
            addComponent(process);
            
            Label title = new Label();
            if (taskTitle.length() > 35) {
                title.setValue(taskTitle.substring(0, 35).replaceAll("\\<.*?\\>", "") + "...");
            } else {
                title.setValue(taskTitle.replaceAll("\\<.*?\\>", ""));
            }
            title.setWidth("-1px");
            title.setStyleName(STYLENAME + "-title");
//            title.setStyleName(Reindeer.LABEL_SMALL);
            addComponent(title);
            
            Label description = new Label();
            description.setStyleName(STYLENAME + "-description");
            if (taskDescription.length() > 80) {
                description.setValue(taskDescription.substring(0, 80).replaceAll("\\<.*?\\>", "") + "...");
            } else {
                description.setValue(taskDescription.replaceAll("\\<.*?\\>", ""));
            }
//            description.setStyleName(Reindeer.LABEL_SMALL);
            addComponent(description);
        } catch (Exception ex) {
            Logger.getLogger(TaskButton.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public LightTaskInstance getTask() {
        return task;
    }
    
        private ThemeResource getResource(){
        ThemeResource icon = null;
        if (!task.isTaskAssigned()) {
            icon = new ThemeResource("icons/email.png");
        } else if (task.getState().equals(ActivityState.SUSPENDED) && task.getPriority() == 0) {
            icon = new ThemeResource("icons/pause_normal.png");
        } else if (task.getState().equals(ActivityState.SUSPENDED) && task.getPriority() == 1) {
            icon = new ThemeResource("icons/pause_high.png");
        } else if (task.getState().equals(ActivityState.SUSPENDED) && task.getPriority() == 2) {
            icon = new ThemeResource("icons/pause_urgent.png");
        } else if (task.getState().equals(ActivityState.EXECUTING) && task.getPriority() == 0) {
            icon = new ThemeResource("icons/arrow_right_normal.png");
        } else if (task.getState().equals(ActivityState.EXECUTING) && task.getPriority() == 1) {
            icon = new ThemeResource("icons/arrow_right_high.png");
        } else if (task.getState().equals(ActivityState.EXECUTING) && task.getPriority() == 2) {
            icon = new ThemeResource("icons/arrow_right_urgent.png");
        } else if (task.getState().equals(ActivityState.READY) && task.getPriority() == 0) {
            icon = new ThemeResource("icons/arrow_right_normal.png");
        } else if (task.getState().equals(ActivityState.READY) && task.getPriority() == 1) {
            icon = new ThemeResource("icons/arrow_right_high.png");
        } else if (task.getState().equals(ActivityState.READY) && task.getPriority() == 2) {
            icon = new ThemeResource("icons/arrow_right_urgent.png");
        } else {
            icon = new ThemeResource("icons/empty.png");
        }
        return icon;
    }
}
